Imports System.Configuration.ConfigurationManager
Imports System.Xml

Partial Class Admin_UserList
    Inherits System.Web.UI.Page

    Private _data_user As DS_user

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then
            LoadListaUtenti()
        End If
        _data_user = Session.Item("DS_user")
        If _data_user.TBL_user.Rows.Count = 0 Then
            Session.Item("USR_selected") = -1
            Response.Redirect("UserManagement.aspx")
        End If

    End Sub

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        Dim dv_usr As Data.DataView

        dv_usr = New Data.DataView(_data_user.TBL_user)
        dv_usr.RowFilter = "USR_codice > 0"
        dv_usr.Sort = "USR_userid"
        GRV_userlist.DataSource = dv_usr
        GRV_userlist.DataBind()

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        Session.Item("USR_selected") = -1
        Response.Redirect("UserManagement.aspx")

    End Sub

    Protected Sub GRV_userlist_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GRV_userlist.RowEditing

        Session.Item("USR_selected") = _data_user.TBL_user.FindByUSR_codice(GRV_userlist.DataKeys(e.NewEditIndex).Value).USR_codice
        Response.Redirect("UserManagement.aspx")

    End Sub

    Private Sub LoadListaUtenti()
        ' Carico la lista degli utenti dell'applicazione eliminando l'utente con codice 0 in 
        ' quanto utente di default per consentire l'accesso anche in caso di errori.

        Session.Item("DS_user") = CType(Session.Item("DBA"), DBAccess).ReadDS_User()

    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim lDeleted As Boolean = False
        Dim dr_usr As DS_user.TBL_userRow

        For Each Grv_Row As GridViewRow In GRV_userlist.Rows
            If CType(Grv_Row.FindControl("CHB_select"), CheckBox).Checked Then
                dr_usr = _data_user.TBL_user.FindByUSR_codice(GRV_userlist.DataKeys(Grv_Row.RowIndex).Value)
                dr_usr.Delete()
                lDeleted = True
            End If
        Next
        If lDeleted Then
            CType(Session.Item("DBA"), DBAccess).WriteDS_User(_data_user)
            '_data_user.TBL_user.AcceptChanges()
            '_data_user.WriteXml(Server.MapPath(AppSettings.Get("TBLUserList")))
        End If
        Session.Item("DS_user") = _data_user

    End Sub

    Public Function GetImageStatusUser(ByVal lUserAbilitato As Boolean) As String
        Dim cRtn As String

        If lUserAbilitato Then
            cRtn = "blt_green.jpg"
        Else
            cRtn = "blt_red.jpg"
        End If
        Return String.Concat("..\images\", cRtn)

    End Function

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim lUpdated As Boolean = False
        Dim dr_usr As DS_user.TBL_userRow

        For Each Grv_Row As GridViewRow In GRV_userlist.Rows
            If CType(Grv_Row.FindControl("CHB_select"), CheckBox).Checked Then
                dr_usr = _data_user.TBL_user.FindByUSR_codice(GRV_userlist.DataKeys(Grv_Row.RowIndex).Value)
                dr_usr.USR_password = AppSettings.Get("DefaultPassword")
                dr_usr.USR_def_pwd = True
                lUpdated = True
            End If
        Next
        If lUpdated Then
            _data_user.TBL_user.AcceptChanges()
            CType(Session.Item("DBA"), DBAccess).WriteDS_User(_data_user)
            '  _data_user.WriteXml(Server.MapPath(AppSettings.Get("TBLUserList")))
        End If
        Session.Item("DS_user") = _data_user

    End Sub

End Class
